from utils.Logger import LogPrint
from db.DatabaseWrapper import DatabaseWrapper

class DbmsDao:
    
      
    #获取数据库信息
    def get_db_info():
        try:

            database_wrapper = DatabaseWrapper()
            res=database_wrapper.select('t_dbms_info')

            if len(res) > 0:
                return res
        except Exception as e:
            print('get_pay_type_from_classify [Error]: ' + str(e))   
        return None
    
    #根据id获取数据库信息
    def get_db_info_by_id(c_id):
        try:

            database_wrapper = DatabaseWrapper()
            res=database_wrapper.select('t_dbms_info','*',condition=f"c_id='{c_id}'")

            if len(res) > 0:
                return res
        except Exception as e:
            print('get_pay_type_from_classify [Error]: ' + str(e))   
        return None

    #插入数据库数据
    ###
                #  name TEXT,
                # dbms_type TEXT,
                # host TEXT,
                # port INTEGER,
                # user_name TEXT,
                # password TEXT,
                # remark TEXT
        ###
    def insert_db_info(name,dbms_type,host,port,user_name,password,remark):
        if name is None or host is None or port is None or user_name is None or password is None:
            return False
        try:

            database_wrapper = DatabaseWrapper()
            data={}

            data['name']=name
            data['dbms_type']=dbms_type
            data['host']=host
            data['port']=port
            data['user_name']=user_name
            data['password']=password

            if remark:
                data['remark']=remark
            res=database_wrapper.insert('t_dbms_info',data)
            
            return True
        except Exception as e:
            print('t_dbms_info [Error]: ' + str(e))   
        return False  
    
    #修改数据
    def update_db_info(c_id,name,dbms_type,host,port,user_name,password,remark):
        if c_id is None or name is None or host is None or port is None or user_name is None or password is None:
            return False
        try:

            database_wrapper = DatabaseWrapper()
            data={}

            data['name']=name
            data['dbms_type']=dbms_type
            data['host']=host
            data['port']=port
            data['user_name']=user_name
            data['password']=password

            if remark:
                data['remark']=remark
            res=database_wrapper.update('t_dbms_info',data,f"c_id='{c_id}'")
            
            return True
        except Exception as e:
            print('t_dbms_info [Error]: ' + str(e))   
        return False 